<!DOCTYPE HTML>
<html>
<HEAD>
<meta charset="utf-8">
<TITLE>增加/修改类别</TITLE>
<script src="js/jquery/jquery-1.3.2.js"></script>

<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

    //开启session
    session_start();
    
    //如果session为空，则退出
    if(!$_SESSION['username']){
     echo "<script>alert('登陆失败');
         history.go(-1);</script>";   
    }
    //new数据库操作对象
    include_once('../DB.php');
    $DB =  new DB();
    
    if($_GET["action"]){     
    $action=$_GET["action"];
     //获取ID号
    $id=intval($_GET["id"]);
    $res_list = $DB->get_all("select * from zhuangxiu where id = $id"); 
  
   }  
   
   
   
   //上传文件类型列表  
$uptypes=array(  
    'image/jpg',  
    'image/jpeg',  
    'image/png',  
    'image/pjpeg',  
    'image/gif',  
    'image/bmp',  
    'image/x-png'  
);  
  
$max_file_size=2000000;     //上传文件大小限制, 单位BYTE  
$destination_folder="/data/wechat/public_html/upload/img/"; //上传文件路径 
   
   
   
    //如果点击了提交按钮    
    if(isset($_POST['submit'])){
        
        
    $file = $_FILES["upfile"];  
    //图片文件链接
    $picFileName = "";
    if (!is_uploaded_file($_FILES["upfile"]["tmp_name"]))  
    //是否存在文件  
    {  
        // echo "图片不存在，文件上传失败!";  
        // exit;  
    }     
   //检查文件大小  
   if($max_file_size < $file["size"]) 
    {  
        //echo "<script>alert('文件太大!');</script>";  
        //exit;  
     } 
     if(!in_array($file["type"], $uptypes))  
    //检查文件类型  
    {  
        //echo "<script>alert('文件类型不符!');</script>".$file["type"];  
        //exit;  
    }  
    if(!file_exists($destination_folder))  
    {  
        if(mkdir($destination_folder)){
            echo "<script>alert('成功创建文件夹');</script>";  
        }else{
            echo "<script>alert('创建文件夹失败');</script>";  
            //exit;  
        }
    }  
    
    $filename=$file["tmp_name"];  
    $image_size = getimagesize($filename);  
    $pinfo=pathinfo($file["name"]);  
    $ftype=$pinfo['extension'];  
    $destination = $destination_folder.time().".".$ftype;  
    
    if (file_exists($destination) && $overwrite != true)  
    {  
        echo "<script>alert('同名文件已经存在了');</script>";  
      //  exit;  
    }  
  
    if(!move_uploaded_file ($filename, $destination))  
    {  
        echo "<script>alert('移动文件出错');</script>";  
        //exit;  
    }  
    echo "<script>alert($filename );</script>"; 
    if($ftype!=null){
    //保存图片文件的链接地址
      $picFileName= "http://wechat.zhijia365.com/upload/img/".time().".".$ftype; 
    }else{
        $picFileName = "";    
    }
    
    
    
    
    //获取数据
     $contentdata = array('content'=>$_POST['content'],
    'type'=>$_POST['type'],"icon"=>$picFileName);
    
     
     
     //判断是不是内容是不是已经存在
     $content = $_POST['content'] ; 
     $res_list = $DB->get_all("select * from zhuangxiu"); 
    
     foreach($res_list as $value){
       if($value['content']== $content ){
           echo "<script>alert('您已经添加过此类别');location.reload();</script>";
           exit;
       }
     }
     
    //获取操作信息,如果是进行了更新操作
    if($_GET["action"]){
         
    $action=$_GET["action"];
     //获取ID号
    $id=intval($_GET["id"]);
    $bool = $DB->update("zhuangxiu",$contentdata ,"id ={$id}"); 
    if($bool){
        echo "<script>alert('数据更新操作成功！');location='contentList.php';</Script>";
       }
   }
    //说明不是更新操作，是增加操作
    else{
         
     $bool = $DB->insert("zhuangxiu",$contentdata); 
     if($bool){
         echo "<script>alert('数据添加操作成功！');location='contentList.php';</Script>";
         }
     }
}
?>

</HEAD>
    <h1>增加/修改类别</h1>    
    <a href="contentList.php" style="text-decoration:none;">类别列表</a></br></br>
      <form action="" method="post" enctype="multipart/form-data">
        内容：<input type="text" name="content" value="<?=$res_list[0][content]?>">ps:不能添加重复的内容</br></br>
        类别: <select name="type" class="type">  
                <option value ="1">装修前准备</option>  
                <option value ="2">建材选购</option>  
                <option value= "3">施工经验</option>  
              </select><br></br>
              
         图标：<input type="file" name="upfile"></br></br>
            
           <?php
           //设置select的type
             echo   
           "<script type='text/javascript'>
                $('.type').val('{$res_list[0][type]}');
            </script>";          
           ?>
         
        <input type="hidden" name="time" value="<?php echo $nowTime;?>"> </br>
       
          
        <input type="submit" name="submit" value="提交">
        
    </form>
</body>
</html>
